package pchi.pchicamera;

import pchi.pchicamera.adapters.AlbumAdapter;
import pchi.pchicamera.data.Contants;
import pchi.pchicamera.data.PhotosData;
import pchi.pchicamera.models.PhotoModel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class AlbumActivity extends Activity implements OnItemClickListener {

	GridView grid;
	PhotosData album;
	BaseAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_album);
		album = Apps.getPhotosData();
		album.load(getApplicationContext());
		grid = (GridView) findViewById(R.id.album_gridview);

		this.adapter = new AlbumAdapter(AlbumActivity.this, this.album);
		grid.setAdapter(this.adapter);
		grid.setOnItemClickListener(this);
		registerForContextMenu(grid);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo cmi = (AdapterContextMenuInfo) menuInfo;

		int id = v.getId();
		if (id == R.id.album_gridview) {
			menu.add(1, cmi.position, 0, R.string.delete_photo);
			menu.add(2, cmi.position, 0, R.string.edit);
			menu.add(3, cmi.position, 0, R.string.composite);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int id = item.getGroupId();
		switch (id) {
		case 1:
			int position = item.getItemId();
			PhotoModel photo = this.album.getPhotos().get(position);
			Apps.getPhotosData().deletePhoto(photo);

			this.adapter.notifyDataSetChanged();
			break;
		case 2:
			int pos = item.getItemId();
			Bundle b = new Bundle();
			PhotoModel photoEditor = this.album.getPhotos().get(pos);
			b.putString("imgPath", photoEditor.getPath());
			Intent editIntent = new Intent(AlbumActivity.this,
					EdittingImagesActivity.class);
			editIntent.putExtra("imgPreview", b);
			startActivity(editIntent);
			break;

		case 3:
			int po = item.getItemId();

			PhotoModel photoComposite = this.album.getPhotos().get(po);

			Intent intent = new Intent();
			intent.putExtra("MESSAGE", photoComposite.getPath());

			setResult(2, intent);

			finish();// finishing activity

			break;

		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.adapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position,
			long Id) {
		Intent intent = new Intent(AlbumActivity.this,
				FullScreenImageActivity.class);
		intent.putExtra(Contants.Position, position);
		startActivity(intent);

	}
}
